package com.example.myapplication.tack_09.adapter;

import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_09.data.ArrangeData;
import com.example.myapplication.tack_09.data.CkjkData;
import com.example.myapplication.tack_09.data.FenLeiData;
import com.example.myapplication.utils.OkHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class ArrangeAdapter extends RecyclerView.Adapter<ArrangeAdapter.My> {
    private List<ArrangeData.RowsDTO> list;
    private Context context;
    private View view;
    private JSONObject jsonObject;
    private Handler handler;
    public ArrangeAdapter(List<ArrangeData.RowsDTO> list, Context context) {
        this.list = list;
        this.context = context;
        handler = new Handler();
    }
    private  void ckjk(String json) {
        String url = "/prod-api/api/public-welfare/donate-record";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, CkjkData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CkjkData ckjkData = (CkjkData)obj;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ckjAdapter(ckjkData);
                    }
                });
            }
            @Override
            public void onFailure(String msg) {

            }
        },json);
    }



    private void ckjAdapter(CkjkData ckjkData) {
        if(ckjkData.getCode() == 200){
            Toast.makeText(context,"捐款成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,ckjkData.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }
    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_09_list,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL + list.get(position).getImgUrl()).into(holder.imags);
        holder.name.setText(list.get(position).getName());
        holder.donateCount.setText(String.valueOf(list.get(position).getDonateCount()));
        String id = String.valueOf(list.get(position).getId());
        holder.jkcg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable text = holder.edi.getText();
                String s = String.valueOf(text);
                try {
                    jsonObject.put("activityId",id);
                    jsonObject.put("donateMoney",s);
                    String s1 = jsonObject.toString();
                    ckjk(s1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        holder.bz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.line.setVisibility(View.VISIBLE);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imags;
        private TextView name;
        private TextView donateCount;
        private TextView bz;
        private TextView jkcg;
        private LinearLayout line;
        private EditText edi;
        public My(@NonNull View itemView) {
            super(itemView);
            imags = view.findViewById(R.id.imags);
            name = view.findViewById(R.id.name);
            bz = view.findViewById(R.id.bz);
            edi = view.findViewById(R.id.edi);
            donateCount = view.findViewById(R.id.donateCount);
            jkcg = view.findViewById(R.id.jkcg);
            line = view.findViewById(R.id.line_2);
            jsonObject = new JSONObject();
        }
    }
}
